<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="CONTEXT_MENUS_KEY" />

<%-- Acquire set of context menus for each table row --%>
<c:set var="contextMenus" value="${requestScope[pageScope.CONTEXT_MENUS_KEY]}" />

<bean:size collection="${contextMenus}" id="contextMenusSize"/>
<c:if test="${pageScope.contextMenusSize > 0}">
    <%-- Create empty layers according to number  of items in the first row --%>
    <c:set var="contextMenu" value="${contextMenus[0]}" />

    <%-- Create menu layout --%>
    <atleap:iterator items="${contextMenu.asPlainList}" var="item" scope="request" statusVar="status" >
        <c:choose>
            <c:when test="${status.first}">
                <div class="contextMenu" id="${contextMenu.id}" style="position: absolute; z-index: 10; visibility: hidden;">
                    <atleap:iterator items="${item.childItems}" var="CONTEXT_MENU_ITEM_KEY" scope="request">
                        <jsp:include page="/WEB-INF/pages/core/contextMenuItem.jsp" />
                    </atleap:iterator>
                </div>
                <script language="javascript" type="text/javascript">
                    contextMenuDivIds[contextMenuDivIds.length] = "${contextMenu.id}";
                </script>
            </c:when>
            <c:otherwise>
                <div class="contextMenu" id="${item.id}__childItems" style="position: absolute;">
                    <atleap:iterator items="${item.childItems}" var="CONTEXT_MENU_ITEM_KEY" scope="request">
                        <jsp:include page="/WEB-INF/pages/core/contextMenuItem.jsp" />
                    </atleap:iterator>
                </div>
                <script language="javascript" type="text/javascript">
                    contextMenuDivIds[contextMenuDivIds.length] = "${item.id}__childItems";
                </script>
            </c:otherwise>
        </c:choose>
    </atleap:iterator>
    
    <%-- Convert server-side menu objects to Javascript --%>
    <script language="javascript" type="text/javascript">
        // create holder
        var ${contextMenu.id} = new ContextMenuHolder();
        <atleap:iterator items="${contextMenus}" var="menu">
            ${contextMenu.id}.addMenu(${menu.javascriptCode});
        </atleap:iterator>
        // save holder in array of context menus
        contextMenus[contextMenus.length] = ${contextMenu.id};
    </script>
</c:if>